package com.nine.pesys.model.action;

import com.nine.pesys.base.BaseAction;
import com.nine.pesys.model.entity.EquipmentDetail;
import com.nine.pesys.model.entity.SportField;
import com.nine.pesys.model.entity.SportFieldDetail;
import com.nine.pesys.util.annotation.Privilege;
import com.nine.pesys.util.common.PageData;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * Created by L先森 on 2014/12/26.
 */
@Controller
@Scope("prototype")
@Namespace("/fieldDetail")
@ParentPackage("my_struts")
@Results({
        @Result(name = "bsAddField",type = "redirectAction",location = "/allField.do"),
        @Result(name = "bsAllField",location = "/admin/field/field_list.jsp"),
})
@Privilege
public class SportFieldDetailAction extends BaseAction<SportFieldDetail>{

    private String fieldName;
    private Float price;

    /**
     * 场地新增
     * (如果之前有这个种场就场地数加一，没有就创建一个新的equipment，属性以第一次填写的数据为准)
     *          需要的参数:
     * @return
     * @throws Exception
     */
    @Privilege("super")
    @Action("/addField")
    public String addField() throws Exception{
        SportField sportField = new SportField();
        if (fieldName!=null)
            sportField.setName(fieldName);
        sportFields = this.sportFieldService.getByCondition(sportField);
        if (sportFields!=null&&sportFields.size()>0){
            sportField = sportFields.get(0);
            sportField.setTotalNum(sportField.getTotalNum()+1L);
            sportField.setLeftNum(sportField.getLeftNum()+1L);
            this.sportFieldService.update(sportField);
        }else {
            System.out.println("===============新建========");
            sportField = new SportField();
            sportField.setName(fieldName);
            sportField.setPrice(price);
            sportField.setTotalNum(1L);
            sportField.setLeftNum(1L);
            this.sportFieldService.save(sportField);
        }
        model.setSportField(sportField);
        this.sportFieldDetailService.save(model);
        return "bsAddField";
    }

    /**
     * 查询所有用户
     * @return
     * @throws Exception
     */
    @Action("/allField")
    public String allField() throws  Exception{
        list = this.sportFieldDetailService.findAll();
        if (list!=null&&list.size()>0) {
            System.out.println(list.size()+"===="+pageNo+"====="+list);
            PageData<SportFieldDetail> pageData = new PageData<SportFieldDetail>(list.size(), pageNo, pageSize, list);
            if (pageNo>pageData.getTotalPageCount())
                pageNo = pageData.getTotalPageCount();
//            equipments = this.equipmentService.getByPage(pageSize,pageNo);
            list = this.sportFieldDetailService.getByPage(pageSize,pageNo);
            ActionContext.getContext().put("fiDL",list);
            ActionContext.getContext().put("pageData",pageData);
            System.out.println(list.size());
            System.out.println(pageData.toString());
            for (SportFieldDetail sportFieldDetail:list){
                System.out.println(sportFieldDetail.toString());
            }
        }
        return "bsAllField";
    }


    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }
}
